package cn.iocoder.yudao.module.biz.service.zmdeduction;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.zmdeduction.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.zmdeduction.ZmDeductionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.biz.dal.mysql.zmdeduction.ZmDeductionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 扣款报 Service 实现类
 *
 * @author 支付免押
 */
@Service
@Validated
public class ZmDeductionServiceImpl implements ZmDeductionService {

    @Resource
    private ZmDeductionMapper zmDeductionMapper;

    @Override
    public Long createZmDeduction(ZmDeductionSaveReqVO createReqVO) {
        // 插入
        ZmDeductionDO zmDeduction = BeanUtils.toBean(createReqVO, ZmDeductionDO.class);
        zmDeductionMapper.insert(zmDeduction);
        // 返回
        return zmDeduction.getId();
    }

    @Override
    public void updateZmDeduction(ZmDeductionSaveReqVO updateReqVO) {
        // 校验存在
        validateZmDeductionExists(updateReqVO.getId());
        // 更新
        ZmDeductionDO updateObj = BeanUtils.toBean(updateReqVO, ZmDeductionDO.class);
        zmDeductionMapper.updateById(updateObj);
    }

    @Override
    public void deleteZmDeduction(Long id) {
        // 校验存在
        validateZmDeductionExists(id);
        // 删除
        zmDeductionMapper.deleteById(id);
    }

    private void validateZmDeductionExists(Long id) {
        if (zmDeductionMapper.selectById(id) == null) {
            throw exception(ZM_DEDUCTION_NOT_EXISTS);
        }
    }

    @Override
    public ZmDeductionDO getZmDeduction(Long id) {
        return zmDeductionMapper.selectById(id);
    }

    @Override
    public PageResult<ZmDeductionDO> getZmDeductionPage(ZmDeductionPageReqVO pageReqVO) {
        return zmDeductionMapper.selectPage(pageReqVO);
    }

}